package ru.tulupov.collectionsbenchmark;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import ru.tulupov.collectionsbenchmark.model.Car;
import ru.tulupov.collectionsbenchmark.util.CSVParser;
import ru.tulupov.collectionsbenchmark.util.CarBuilder;

public class Main {
	private static final Logger LOGGER = Logger.getLogger(Main.class);

	public static void main(String[] args) {
		InputStream is = null;
		try {
			LOGGER.info("Starts loading file.");
			is = new FileInputStream("csv.txt");
			CSVParser parser = new CSVParser(is);
			List<String[]> list = parser.parse();
			CarBuilder builder = new CarBuilder();
			List<Car> cars = new ArrayList<Car>();
			for (String[] data : list) {
				try {
					cars.add(builder.build(data));
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
			}

			Car[] array = new Car[cars.size()];
			cars.toArray(array);
			LOGGER.info("File loaded.");
			LOGGER.info("Starts testing.");
			TestRunner runner = new TestRunner();
			runner.start();
			LOGGER.info("Testing ended.");

		} catch (FileNotFoundException e) {

		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {

				}
			}
		}

	}
}
